package com.baosight.risk.service.riim.impl;

import com.baosight.risk.api.riim.RiimRiskGoalService;
import com.baosight.risk.base.service.BaseServiceImpl;
import com.baosight.risk.common.bean.BeanUtils;
import com.baosight.risk.common.utils.StringUtils;
import com.baosight.risk.mapper.riim.RiimRiskGoalDetailMapper;
import com.baosight.risk.mapper.riim.RiimRiskGoalMapper;
import com.baosight.risk.service.riim.entity.RiimRiskGoal;
import com.baosight.risk.service.riim.entity.RiimRiskGoalDetail;
import com.baosight.risk.service.riim.vo.RiimRiskGoalVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author RayWade
 * @create 2019 - 08 - 20 17:09
 */
@Service
@Slf4j
public class RiimRiskGoalServiceImpl extends BaseServiceImpl<RiimRiskGoal>
        implements RiimRiskGoalService {

    @Autowired
    private RiimRiskGoalMapper goalMapper;

    @Autowired
    private RiimRiskGoalDetailMapper goalDetailMapper;

    @Override
    public RiimRiskGoalVO saveGoal(RiimRiskGoalVO goalVO) {
        RiimRiskGoalVO riskGoalVO = new RiimRiskGoalVO();
        if (StringUtils.isNotEmpty(goalVO.getId())) {
            // 修改项目主表主表
            riskGoalVO = (RiimRiskGoalVO) BeanUtils.updateBean(goalVO);
            update(riskGoalVO);
            getGoalDetail(goalVO);
        } else {
            Integer rowOrder;
            Integer maxRowOrder = goalMapper.queryMax(goalVO.getImportGuid());
            if (maxRowOrder == null) {
                rowOrder = 1;
            } else {
                rowOrder = Integer.valueOf(maxRowOrder) + 1;
            }
            goalVO.setRowOrder(rowOrder);
            riskGoalVO = (RiimRiskGoalVO) BeanUtils.initBean(goalVO);
            goalMapper.insert(riskGoalVO);
            getGoalDetail(goalVO);
        }
        return riskGoalVO;
    }

    @Override
    public Integer deleteGoal(List<String> ids) {
        ids.forEach(id -> {
            // 先删除子表数据
            goalDetailMapper.deleteByImportGuidAndGoalId(null, id);
            goalMapper.deleteByPrimaryKey(id);
        });
        return null;
    }

    @Override
    public void updateRowOrder(RiimRiskGoal riskGoal) {
        List<RiimRiskGoal> goalList = goalMapper.selectByImportGuid(riskGoal.getImportGuid());
        if (CollectionUtils.isNotEmpty(goalList)) {
            List<String> ids = goalList.stream().map(RiimRiskGoal::getId).collect(Collectors.toList());
            RiimRiskGoal goal1 = get(riskGoal.getId());
            int i = ids.indexOf(riskGoal.getId());
            if ("up".equals(riskGoal.getExt1())) {
                i--;
            } else {
                i++;
            }
            if(i < ids.size() && i >= 0){
                RiimRiskGoal goal2 = get(ids.get(i));
                int rowOrder1 = goal1.getRowOrder();
                int rowOrder2 = goal2.getRowOrder();
                goal1.setRowOrder(rowOrder2);
                update(goal1);
                goal2.setRowOrder(rowOrder1);
                update(goal2);
            }
        }
    }

    private void getGoalDetail(RiimRiskGoalVO goalVO) {
        if (StringUtils.isNotEmpty(goalVO.getGoalDetailList())) {
            goalDetailMapper.deleteByImportGuidAndGoalId(goalVO.getImportGuid(), goalVO.getId());
            for (RiimRiskGoalDetail goalDetail : goalVO.getGoalDetailList()) {
                goalDetail = (RiimRiskGoalDetail) BeanUtils.initBean(goalDetail);
                goalDetail.setGoalId(goalVO.getId());
                goalDetail.setImportGuid(goalVO.getImportGuid());
                goalDetailMapper.insert(goalDetail);
            }
        }

    }

}
